package com.example.stray_animal_management_system.admin.Statistics;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.stray_animal_management_system.commonu.service.RescueInfoService;
import com.example.stray_animal_management_system.entity.RescueInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: panpan
 * @time: 2025/11/7
 */
@RestController
@RequestMapping("/api/admin/statistics")
public class RescueInfoStatisticsController {

    @Autowired
    private RescueInfoService rescueInfoService;

    /**
     * 获取所有救助记录数量
     */
    @GetMapping("/rescueInfoCount")
    public Map<String, Object> getRescueInfoCount() {
        Map<String, Object> res = new HashMap<>();
        long count = rescueInfoService.count();  // 获取所有救助记录数
        res.put("code", 200);
        res.put("data", count);
        return res;
    }

    /**
     * 按状态统计救助记录数量
     */
    @GetMapping("/rescueInfoStatusCount")
    public Map<String, Object> getRescueInfoStatusCount() {
        Map<String, Object> res = new HashMap<>();

        // 使用 QueryWrapper 结合原生 SQL 查询
        List<Map<String, Object>> statusCount = rescueInfoService.getBaseMapper()
                .selectMaps(new QueryWrapper<RescueInfo>()
                        .select("status, COUNT(*) as count")  // 使用原生 SQL 查询
                        .groupBy("status")  // 按 status 分组
                );

        res.put("code", 200);
        res.put("data", statusCount);
        return res;
    }


    /**
     * 按上报人统计救助记录数量
     */
    @GetMapping("/rescueInfoReporterCount")
    public Map<String, Object> getRescueInfoReporterCount() {
        Map<String, Object> res = new HashMap<>();

        // 使用 QueryWrapper 结合原生 SQL 查询
        List<Map<String, Object>> reporterCount = rescueInfoService.getBaseMapper()
                .selectMaps(new QueryWrapper<RescueInfo>()
                        .select("reporter_id, COUNT(*) as count")  // 使用原生 SQL 查询
                        .groupBy("reporter_id")  // 按 reporter_id 分组
                );

        res.put("code", 200);
        res.put("data", reporterCount);
        return res;
    }

}
